package com.googlecode.lohr.parser.combinators.matcher.common;


import com.googlecode.lohr.parser.combinators.MatchError;
import com.googlecode.lohr.parser.combinators.matcher.MatchContext;
import com.googlecode.lohr.parser.combinators.matcher.Matcher;
import com.googlecode.lohr.parser.combinators.node.StringNode;

/**
 * Matches everything from the beginning marker (passed in the constructor) 
 * to the end of the line. 
 * @author Ted Stockwell
 */
public class SingleLineCommentMatcher extends Matcher
{
	String _marker;
	public SingleLineCommentMatcher(String marker) {
		_marker= marker;
	}

	@Override
	public void match(MatchContext ctx, int start) 
	{
		final String input= ctx.getInput();
		if (input.startsWith(_marker, start)) {
			int len= input.length();
			int i= start+_marker.length();
			while (i < len && input.charAt(i) != '\n')
				i++;
			if (i < len)
				i++;
			if (i < len && input.charAt(i) == '\r')
				i++;
			StringNode node= new StringNode(this, start, i, input.substring(start, i));
			ctx.matchFound(this, start, node);
			ctx.matchComplete(this, start);
			return;
		}
		ctx.matchComplete(this, start, new MatchError("Expected whitespace", start));
	}

	@Override
	public String getLabel()
	{
		return "single line comment";
	}
}
